package org.glickr


class Utils {

    // todo url processor die dubbele attributen eruit haalt

     static String base58encode(Long number) {
        final String alphabet = ((0..9) + ('a'..'z') + ('A'..'Z') - [0,'O','I','l']).join()
        assert alphabet.length() == 58

        String encoded = ""
        while (number >= 58) {
            Long div = (number/58)
            Integer mod = (number-(58*div))
            encoded = alphabet[mod] + encoded
            number = div
        }
        if (number>0){
            encoded = alphabet[number as Integer] + encoded
        }
        return encoded
    }

	static Long base58decode(String text){
        final String alphabet = ((0..9) + ('a'..'z') + ('A'..'Z') - [0,'O','I','l']).join()
        assert alphabet.length() == 58

        Long decoded = 0
        Long multi = 1
        while (text.length() > 0) {
            String digit = text.substring(text.length()-1)
            decoded = decoded + multi * alphabet.lastIndexOf(digit)
            multi   = multi * 58
            text    = text.substring(0, text.length()-1)
        }
        return decoded;
	}
}
